home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / WTEK0593.ZIP;1 / CALLBACK.ZIP / TIMERTST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-10  |  4.9 KB  |  203 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //  TIMERTST.CPP
  4. //
  5. //  Test Application which uses the OTimer class.  Note: This requires
  6. //  Borland C++ (it uses the Object Windows Library).
  7. //
  8. //--------------------------------------------------------------------------
  9.  
  10. #include "otimer.h"
  11. #ifndef __OWL_H
  12. #include <owl.h>
  13. #endif
  14. #ifndef __WINDOW_H
  15. #include <window.h>
  16. #endif
  17.  
  18.  
  19. //--------------------------------------------------------------------------
  20. //
  21. //  Timer test class
  22. //  
  23. //--------------------------------------------------------------------------
  24. class TestTimer : public OTimer {
  25. private:
  26.     int     wCount;
  27.     HWND    hwndOwner;
  28.     RECT    rDisplay;
  29.  
  30.     virtual void timerHit(DWORD dwTime);
  31. public:
  32.     TestTimer(HWND hwnd, RECT &r, UINT mSec)
  33.         : hwndOwner(hwnd), rDisplay(r)
  34.     {
  35.         wCount = 0;
  36.         go(mSec);
  37.     }
  38.  
  39.     void    draw(HDC drawDC);
  40.  
  41. };
  42.  
  43.  
  44.  
  45. //--------------------------------------------------------------------------
  46. //
  47. //  TestTimer :: timerHit
  48. //
  49. //  Respond to timer callback
  50. //
  51. //--------------------------------------------------------------------------
  52. void TestTimer::timerHit(DWORD)
  53. {
  54.     ++wCount;
  55.  
  56.     HDC wndDC = GetDC(hwndOwner);
  57.  
  58.     if(wndDC) {
  59.         draw(wndDC);
  60.         ReleaseDC(hwndOwner, wndDC);
  61.     };
  62. }
  63.  
  64. //--------------------------------------------------------------------------
  65. //
  66. //  Perform actual drawing
  67. //
  68. //--------------------------------------------------------------------------
  69. void TestTimer::draw(HDC wndDC)
  70. {
  71.     // this drawing (we hope!) is OK
  72.     char szBuffer[20];
  73.     RECT    rText;
  74.  
  75.     HGDIOBJ hOldFont = SelectObject(wndDC, GetStockObject(SYSTEM_FONT));
  76.     wsprintf(szBuffer, "%5d", wCount);
  77.     ExtTextOut(wndDC, rDisplay.left, rDisplay.top, ETO_OPAQUE,
  78.         &rDisplay, szBuffer, lstrlen(szBuffer), NULL);
  79.  
  80.     SelectObject(wndDC, hOldFont);
  81. }
  82.  
  83.  
  84. //--------------------------------------------------------------------------
  85. //
  86. //  TTimerWindow
  87. //
  88. //--------------------------------------------------------------------------
  89. _CLASSDEF(TTimerWindow)
  90. class TTimerWindow : public TWindow {
  91.     // total test code!
  92.     FARPROC lpProc;
  93.     TestTimer   *timer1,
  94.                 *timer2,
  95.                 *timer3;
  96. public:
  97.     TTimerWindow(PTWindowsObject AParent, LPSTR ATitle);
  98.  
  99.     virtual void SetupWindow();
  100.     virtual void Paint(HDC DC, PAINTSTRUCT& PS);
  101.     virtual void ShutDownWindow();
  102. };
  103.  
  104.  
  105.  
  106. //--------------------------------------------------------------------------
  107. //
  108. //  TTimerWindow :: CONSTRUCTOR
  109. //
  110. //--------------------------------------------------------------------------
  111. TTimerWindow::TTimerWindow(PTWindowsObject AParent, LPSTR ATitle)
  112.   : TWindow(AParent, ATitle)
  113. {
  114. //  AssignMenu("COMMANDS");
  115.     Attr.W  = 400;
  116.     Attr.H = 200;
  117.  
  118.     timer1 = timer2 = timer3 = NULL;
  119. }
  120.  
  121.  
  122. //--------------------------------------------------------------------------
  123. //
  124. //  TTimerWindow :: Paint
  125. //
  126. //--------------------------------------------------------------------------
  127. void TTimerWindow::Paint(HDC DC, PAINTSTRUCT&)
  128. {
  129.     timer1->draw(DC);
  130.     timer2->draw(DC);
  131.     timer3->draw(DC);
  132. }
  133.  
  134.  
  135.  
  136. //--------------------------------------------------------------------------
  137. //
  138. //  TTimerWindow :: SetupWindow
  139. //
  140. //--------------------------------------------------------------------------
  141. void TTimerWindow::SetupWindow()
  142. {
  143.     TWindow::SetupWindow();
  144.     RECT    rLoc;
  145.  
  146.     rLoc.left  = rLoc.top = 0;
  147.     rLoc.right = 100, rLoc.bottom = 20;
  148.  
  149.     timer1 = new TestTimer(HWindow, rLoc, 500);
  150.     rLoc.top += 20, rLoc.bottom +=20;
  151.     timer2 = new TestTimer(HWindow, rLoc, 1000);
  152.     rLoc.top += 20, rLoc.bottom +=20;
  153.     timer3 = new TestTimer(HWindow, rLoc, 750);
  154. }
  155.  
  156.  
  157.  
  158. //--------------------------------------------------------------------------
  159. //
  160. //  TTimerWindow :: ShutDownWindow
  161. //
  162. //--------------------------------------------------------------------------
  163. void TTimerWindow::ShutDownWindow()
  164. {
  165.     if(timer1) delete timer1;
  166.     if(timer2) delete timer2;
  167.     if(timer3) delete timer3;
  168.     TWindow::ShutDownWindow();
  169. }
  170.  
  171.  
  172.  
  173. //-------------------------------------------------------------------
  174. //
  175. //  OWL Stuff to setup the application
  176. //
  177. //-------------------------------------------------------------------
  178. class TTimerApp : public TApplication {
  179. public:
  180.   TTimerApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  181.     LPSTR lpCmdLine, int nCmdShow)
  182.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) 
  183.         {};
  184.   virtual void InitMainWindow();
  185. };
  186.  
  187. void TTimerApp::InitMainWindow()
  188. {
  189.   MainWindow = new TTimerWindow(NULL, Name);
  190. }
  191.  
  192. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  193.   LPSTR lpCmdLine, int nCmdShow)
  194. {
  195.   TTimerApp MyApp("Object Callback Sample", hInstance, hPrevInstance,
  196.                lpCmdLine, nCmdShow);
  197.   MyApp.Run();
  198.   return MyApp.Status;
  199. }
  200.  
  201.  
  202.  
  203.